import { Module, ActionTree, MutationTree, } from 'vuex'
import { IndexStateType, StateTypes, GetterType } from '../types'

const state: IndexStateType = {
  requestCount: {
    count: 0
  },
  a: 999
}

const getters: GetterType= {
  getCount(state) {
    return state.a
  }
}

const mutations: MutationTree<IndexStateType> = {
  increment(state: IndexStateType, clearFlag: boolean = false) {
    if (!clearFlag) {
      state.requestCount.count++
    } else {
      state.requestCount.count = 0
    }
  }
}

const actions: ActionTree<IndexStateType, StateTypes> = {
  increments({ dispatch, commit }) {
    console.log('调用increment');
    commit('increment')
    // dispatch('xxmodule/xxx', xxx, { root: true })
  }
}

const index: Module<IndexStateType, StateTypes> = {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}

export default index
